/********************************************
* Copyrigt (C) rumi
* Module: serv_datapack.h
* History: Created on 2021.8.10
* Version:1.0.0 add ros2 support
* Version:1.0.1 combine circle and crc tools
*********************************************/
#ifndef serv_datapack_H
#define serv_datapack_H
#include <string.h>
#include <stdbool.h>
#include "stdint.h"

#ifdef __cplusplus
#include <iostream>
extern "C" {
#endif

/****************************************************/
/*$$$$$$$$$$$$$$&$     Config    $$$$$$$$$$$$$$$$$$$*/
#define DEVICE_HEAD SLAVE_HEAD
#define TARGET_HEAD MASTER_HEAD
/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/
/****************************************************/

#define PackDataLen  2

#define MASTER_HEAD 0xAA
#define SLAVE_HEAD  0xBB

typedef enum
{
	E_COMFRAMEHEAD_NONE  = 0,
    E_COMFRAMEHEAD_NORMAL,
    E_COMFRAMEHEAD_UPDATE,
    E_COMFRAMEHEAD_STRING,
    
}COM_FRAME_HEAD_ENUM;

typedef enum
{
	E_COMFRAMETAIL_NONE  = 0,
    E_COMFRAMETAIL_JustFloat,
    E_COMFRAMETAIL_CRC16,
    E_COMFRAMETAIL_CRC32,
}COM_FRAME_CRC_ENUM;

typedef enum
{
	E_COMSENSENDSTATE_INIT  = 0,
    E_COMSENSENDSTAT_FRAMEHEAD,
    E_COMSENSENDSTAT_DATA,
    E_COMSENSENDSTAT_FRAMEEND,
    E_COMSENSENDSTAT_LENGTH,

}COM_SEND_STATE_ENUM;

typedef enum
{
    E_COM_SYNC_SEARCH,
    E_COM_HEADER_PROCESS,
    E_COM_DATA_PROCESS,
}COM_PACK_STATE_MACHINE;

typedef struct {
    uint8_t  HeadType;
    uint8_t  TailType;
    uint8_t  HeadLen;
    uint8_t  DataLen;
    uint8_t  Lenindex;
    uint8_t  *pBuff; 
    uint16_t DataMaxLen;       
    uint32_t Size;         
    uint32_t Head;         
    uint32_t Tail;
    COM_PACK_STATE_MACHINE fsm;
} CBuff;

uint16_t CRC16_Check_Code(uint8_t * data, uint16_t length);
uint32_t CRC32_Check_Code(uint8_t *data, uint32_t length);
void CBuff_Init(CBuff *buffer, uint8_t *pBuff, uint32_t size);
uint32_t CBuff_GetLength(const CBuff *buffer);
bool CBuff_Write(CBuff *buffer, const uint8_t *data, uint32_t len);
bool CBuff_Pop(CBuff *buffer, uint8_t *data, uint32_t len);
bool CBuff_Read(CBuff *buffer, uint8_t *data, uint32_t len);
void CBuff_Clear(CBuff *buffer);
bool CBuff_IsEmpty(const CBuff *buffer);
bool CBuff_IsFull(const CBuff *buffer);
uint32_t CBuff_GetFreeSpace(const CBuff *buffer);
uint8_t* Serv_Com_PackageData(COM_FRAME_HEAD_ENUM FrameHead,COM_FRAME_CRC_ENUM  FrameTail,uint8_t *Buffer,uint8_t DataLen);
uint8_t Serv_Com_UnpackFSM(CBuff *lRecive_CBuff, uint8_t *sucReciveData);

#ifdef __cplusplus
}
#endif
#endif  // serv_datapack_H
/********************************************
 * End of file
*********************************************/
